package com.study.talk.app;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;

import com.study.talk.dao.AppDatabase;
import com.study.talk.entity.GroupInfo;
import com.study.talk.entity.User;
import com.study.talk.entity.UserInfo;
import com.study.talk.netty.message.response.LoginResponse;
import com.study.talk.util.AppExecutor;
import com.study.talk.util.Constants;
import com.study.talk.util.EventBusUtil;
import com.study.talk.util.OtherMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App extends Application {

    public LoginResponse loginResponse;
    public User user;
    public UserInfo userInfo;

    public List<UserInfo> friendList;

    public List<GroupInfo> groupList;

    public Boolean loadCache = true;

    public AppDatabase db;

    public int activityCount = 0;

    public boolean isBackground = false;

    private final String TAG = "Application";

    public Map<String, Object> dataMap = new HashMap<>();

    public boolean isLogin() {
        return this.loginResponse != null;
    }

    public String getToken() {
        return needLogin() ? this.loginResponse.getToken() : "";
    }

    public Integer userId() {
        return needLogin() ? this.loginResponse.getUserId() : -1;
    }

    public String getUsername() {
        return needLogin() ? this.loginResponse.getUsername() : "";
    }

    public String getAvatar() {
        return needLogin() ? this.userInfo.getAvatar() : "";
    }

    public void login(LoginResponse loginResponse) {
        this.loginResponse = loginResponse;
        this.user = loginResponse.getUser();
        this.userInfo = loginResponse.getUserInfo();
        this.friendList = loginResponse.getFriendList();
        this.groupList = loginResponse.getGroupList();
    }

    public void logout() {
        this.loginResponse = null;
        this.user = null;
        this.userInfo = null;
        this.friendList = null;
        this.groupList = null;
        getSharedPreferences("account", MODE_PRIVATE).edit().clear().apply();
        AppExecutor.getInstance().FILE_IO().execute(() -> {
            db.groupMessageDao().deleteAll();
            db.messageDao().deleteAll();
        });
    }

    public boolean needLogin() {
        if (!isLogin()) {
            EventBusUtil.sendEvent(new OtherMessage(Constants.NEED_LOGIN));
        }
        return isLogin();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        db = AppDatabase.getInstance(getApplicationContext());
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, Bundle savedInstanceState) {
                //可以在这里add
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                activityCount++;
                if (activityCount == 1 && isBackground) {
                    Log.e(TAG, "onActivityStarted: 回到前台");
                    isBackground = false;
                }
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                activityCount--;
                if (activityCount <= 0 && !isBackground) {
                    isBackground = true;
                    Log.e(TAG, "onActivityStopped: 进入后台");
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                //可以在这里remove
            }
        });
    }
}
